﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity.Validation;
using Backend.JYD.Common;
using Backend.JYD.Model.Auth;

namespace Backend.JYD.EFDAL.DAL
{
    public class MenuInfoDAL : IDAL.Auth.IMenuInfo
    {
        public bool AddMenuInfo(MenuInfo info)
        {
            using (var db = new JydDataContext())
            {
                try
                {
                    db.MenuInfoes.Add(info);
                    return db.SaveChanges() > 0;
                }
                catch (DbEntityValidationException ex)
                {
                    LogHelper.Log(ex);
                    return false;
                }
            }
        }

        public bool UpdateMenuInfo(MenuInfo info)
        {
            using (var db = new JydDataContext())
            {
                try
                {
                    db.Entry<MenuInfo>(info).State = System.Data.Entity.EntityState.Modified;
                    return db.SaveChanges() > 0;
                }
                catch (DbEntityValidationException ex)
                {
                    LogHelper.Log(ex);
                    return false;
                }
            }
        }

        public bool DeleteMenuInfo(string MenuId)
        {
            using (var db = new JydDataContext())
            {
                try
                {
                    var info = db.MenuInfoes.Find(MenuId);
                    db.Entry<MenuInfo>(info).State = System.Data.Entity.EntityState.Deleted;
                    return db.SaveChanges() > 0;
                }
                catch (DbEntityValidationException ex)
                {
                    LogHelper.Log(ex);
                    return false;
                }
            }
        }

        public MenuInfo GetMenuInfoById(string menuId)
        {
            using (var db = new JydDataContext())
            {
                try
                {
                    return db.MenuInfoes.Include("MenuAuthInfoes").Where(p => p.MenuId == menuId).FirstOrDefault();
                }
                catch (DbEntityValidationException ex)
                {
                    LogHelper.Log(ex);
                    return null;
                }
            }
        }
        public IList<MenuInfo> GetAllMenuInfo()
        {
            using (var db = new JydDataContext())
            {
                try
                {
                    return db.MenuInfoes.Include("MenuAuthInfoes").ToList();
                }
                catch (DbEntityValidationException ex)
                {
                    LogHelper.Log(ex);
                    return null;
                }
            }
        }

        public IList<MenuInfo> GetMenuInfoByIds(IList<string> menuIds)
        {
            string[] ids = menuIds.ToArray();
            using (var db = new JydDataContext())
            {
                try
                {
                    var query = db.MenuInfoes.Include("MenuAuthInfoes").Where(p => ids.Contains(p.MenuId));
                    return query.ToList();
                }
                catch (DbEntityValidationException ex)
                {
                    LogHelper.Log(ex);
                    return null;
                }
            }
        }

    }
}
